import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import "package:word_range_client/pojo/vo/UserVo.dart";

import "../bloc/UserBloc.dart";
import "../config/GlobalThemData.dart";
import "general/EventPageView.dart";
// 安卓手机
import "general/GeneralWelcome.dart";
import "general/Profile.dart";
import "phone/PhoneHome.dart";

class PhoneView extends StatefulWidget {
  final bool isDarkMode;

  const PhoneView({super.key, required this.isDarkMode});

  @override
  State<PhoneView> createState() => _PhoneViewState();
}

class _PhoneViewState extends State<PhoneView> {

  @override
  void initState() {
    super.initState();
    BlocProvider.of<UserBloc>(context).add(LoadUser());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: widget.isDarkMode ? GlobalThemData.darkThemeData : GlobalThemData.lightThemeData,
      debugShowCheckedModeBanner: false,
      home: const _Tabs()
    );
  }
}

class _Tabs extends StatefulWidget {
  const _Tabs({super.key});

  @override
  State<_Tabs> createState() => _TabsState();
}

class _TabsState extends State<_Tabs> {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<UserBloc, UserState>(
      builder: (context, state) {
        if (state is UserLoading) {
          return const CircularProgressIndicator();
        } else if (state is UserLoaded) {
          final userViewModel = state.userViewModel;
          UserVo? currentUser = userViewModel.currentUser;
          // 若未登录
          if(currentUser == null){
            return const GeneralWelcome();
          }
          return const _TabsView();
        } else {
          return Container();
        }
      },
    );
  }
}


class _TabsView extends StatefulWidget {
  const _TabsView({super.key});

  @override
  State<_TabsView> createState() => _TabsViewState();
}

class _TabsViewState extends State<_TabsView> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late PageController _pageController;

  int _currentIndex = 0;  // 存储选中了哪一个页面
  final List<Widget> _pages = [
    const PhoneHome(),
    const EventPageView(),
    const Profile(),
  ];

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
    _pageController = PageController(initialPage: _currentIndex);
  }

  @override
  void dispose() {
    _controller.dispose();
    _pageController.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: PageView(
          controller: _pageController,
          onPageChanged: (index){
            setState(() {
              _currentIndex = index;
            });
          },
          children: _pages,
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        unselectedItemColor: Theme.of(context).colorScheme.onPrimary,
        selectedItemColor: Theme.of(context).colorScheme.primary,
        onTap: (index){
          _pageController.animateToPage(
              index,
              duration: const Duration(milliseconds: 200),
              curve: Curves.bounceInOut
          );
          setState(() {
            _currentIndex = index;
          });
        },
        currentIndex: _currentIndex,
        showUnselectedLabels: false,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: "主页",
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.event),
              label: "活动"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              label: "个人中心"
          ),
        ],
      ),
    );
  }
}

